<?php
# @Author: yongleicheng <jetox>
# @Date:   2017-11-21T02:58:02+08:00
# @Email:  admin@weiqisheng.cn
# @Project: WQSCMS
# @Filename: Index.php
# @Last modified by:   jetox
# @Last modified time: 2017-11-21T07:29:27+08:00
# @Copyright: 微企胜网络科技有限公司

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Input;

class Index extends Common
{
    public function _initialize()
    {
        parent::_initialize();
    }
    public function index()
    {
        //导航
        // 获取缓存数据
        $authRule = cache('authRule');
        if (!$authRule) {
            $authRule = db('auth_rule')->where('menustatus=1')->order('sort')->select();
            cache('authRule', $authRule, 3600);
        }
        //声明数组
        $menus = array();
        foreach ($authRule as $key=>$val) {
            $authRule[$key]['href'] = url($val['href']);
            if ($val['pid']==0) {
                if (session('aid')!=1) {
                    if (in_array($val['id'], $this->adminRules)) {
                        $menus[] = $val;
                    }
                } else {
                    $menus[] = $val;
                }
            }
        }
        foreach ($menus as $k=>$v) {
            foreach ($authRule as $kk=>$vv) {
                if ($v['id']==$vv['pid']) {
                    if (session('aid')!=1) {
                        if (in_array($vv['id'], $this->adminRules)) {
                            $menus[$k]['children'][] = $vv;
                        }
                    } else {
                        $menus[$k]['children'][] = $vv;
                    }
                }
            }
        }
        $this->assign('menus', json_encode($menus, true));
        return $this->fetch();
    }
    public function main()
    {
        $version = Db::query('SELECT VERSION() AS ver');
        $config  = [
            'url'             => $_SERVER['HTTP_HOST'],
            'document_root'   => $_SERVER['DOCUMENT_ROOT'],
            'server_os'       => PHP_OS,
            'server_port'     => $_SERVER['SERVER_PORT'],
            'server_ip'       => $_SERVER['SERVER_ADDR'],
            'server_soft'     => $_SERVER['SERVER_SOFTWARE'],
            'php_version'     => PHP_VERSION,
            'mysql_version'   => $version[0]['ver'],
            'max_upload_size' => ini_get('upload_max_filesize')
        ];
        $this->assign('config', $config);
        return $this->fetch();
    }
    public function navbar()
    {
        return $this->fetch();
    }
    public function nav()
    {
        return $this->fetch();
    }
    public function clear()
    {
        $R = RUNTIME_PATH;
        if ($this->_deleteDir($R)) {
            $result['info'] = '清除缓存成功!';
            $result['status'] = 1;
        } else {
            $result['info'] = '清除缓存失败!';
            $result['status'] = 0;
        }
        $result['url'] = url('admin/index/index');
        return $result;
    }
    private function _deleteDir($R)
    {
        $handle = opendir($R);
        while (($item = readdir($handle)) !== false) {
            if ($item != '.' and $item != '..') {
                if (is_dir($R . '/' . $item)) {
                    $this->_deleteDir($R . '/' . $item);
                } else {
                    if (!unlink($R . '/' . $item)) {
                        die('error!');
                    }
                }
            }
        }
        closedir($handle);
        return rmdir($R);
    }

    //退出登陆
    public function logout()
    {
        session(null);
        $this->redirect('login/index');
    }
}
